package ch.ti.kmltomap;

import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import com.google.android.gms.maps.GoogleMap;

import android.util.Log;

public class Folder extends Feature{
	
	private List<Feature> features = null;
	
	public Folder(){}
	
	public Folder(XmlPullParser xpp){
		try{
			this.id = xpp.getAttributeValue(null, "id");
			int eventType = xpp.next();
			String tagName = "";
			while (eventType != XmlPullParser.END_DOCUMENT) {
				
				if(eventType == XmlPullParser.START_TAG){
					if(xpp.getName().equals("Folder")){
						Folder folder = new Folder(xpp);
						this.addFeature(folder);
					}else if(xpp.getName().equals("Placemark")){
						Placemark placemark = new Placemark(xpp);
						this.addFeature(placemark);
					}
					tagName = xpp.getName();
				}else if(eventType == XmlPullParser.TEXT && xpp.getName() != null){
					if(tagName.equals("name")){
						this.name = this.name + xpp.getText().trim();
					}else if(tagName.equals("open")){
						this.open = xpp.getText().trim().equals("1");
					}else if(tagName.equals("Snippet")){
						this.snippet = this.snippet + xpp.getText().trim();
					}else if(tagName.equals("visibility")){
						this.visibility = xpp.getText().trim().equals("1");
					}else if(tagName.equals("address")){
						this.address = this.address + xpp.getText().trim();
					}else if(tagName.equals("phoneNumber")){
						this.phoneNumber = this.phoneNumber + xpp.getText().trim();
					}else if(tagName.equals("description")){
						this.description = this.description + xpp.getText().trim();
					}
				}else if(eventType == XmlPullParser.END_TAG){
					if(xpp.getName().equals("Folder")){
						break;
					}
				}
				eventType = xpp.next();
			}
		}catch (Exception e) {
			Log.e("eee", e.getMessage());
		}
	}
	
	public void addFeature(Feature feature){
		this.checkAndInitFeature();
		this.features.add(feature);
	}
	
	private void checkAndInitFeature(){
		if(this.features == null)
			features = new ArrayList<Feature>();
	}
	
	public void draw(GoogleMap map){
		for (Feature feature : this.features) {
			feature.draw(map);
		}
	}
}
